package com.jsj.isdt.servlet;

import com.alibaba.fastjson.JSON;
import com.jsj.isdt.entity.*;
import com.jsj.isdt.utils.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

public class ServletList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action") == null ? "" : req.getParameter("action");
        switch (action) {
            case "getdglist":
                getDglist(req, resp);
                break;
            case "getdglist1":
                getDglist1(req, resp);
                break;
            case "getdglist2":
                getDglist2(req, resp);
                break;
            case "getdglist3":
                getDglist3(req, resp);
                break;
            default:
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }

    private void getDglist(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select * from user";
            List<User> list = DBUtils.testDemoQueryBeanList(sql, User.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist1(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select * from mes ";
            List<Message> list = DBUtils.testDemoQueryBeanList(sql, Message.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist2(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select * from shop";
            List<Shop> list = DBUtils.testDemoQueryBeanList(sql, Shop.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void getDglist3(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select id,username,phonenum from user where id='1'";
            List<User> list = DBUtils.testDemoQueryBeanList(sql, User.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
  }
